//
//  StudyDetailViewController.m
//  BeautyClother
//
//  Created by dllo on 15/8/31.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "StudyDetailViewController.h"
#import "AsyncConnection.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "CommentCell.h"
#import "ImageSaveViewController.h"

@interface StudyDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,retain) NSMutableArray *array;
@property (nonatomic, retain) UITableView*table;
@property (nonatomic, retain) NSDictionary *dic;

@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, assign) CGFloat offHeight;

@end

@implementation StudyDetailViewController

- (void)dealloc
{
    [_table release];
    [_array release];
    [_dic release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
        self.offHeight = [[UIScreen mainScreen]bounds].size.height / 667;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self beginnection];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(fanhui:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
    // Do any additional setup after loading the view.
}

- (void)fanhui:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)beginnection
{
    
    NSDictionary *dic = @{@"gf":@"iphone",
                          @"gv":@"6.3.0",
                          @"gs":@"640x1136",
                          @"thread_id":self.sid,
                          @"flag":@"",
                          @"state":@"0",
                          @"asc":@"1"};
    [AsyncConnection asyncConnection:STUDY_DETAIL parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dic = [dic objectForKey:@"data"];
        self.array = [self.dic objectForKey:@"items"];
        [self createTable];
    }path:self.sid];
}

- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 69, self.view.frame.size.width, self.view.frame.size.height - 69) style:UITableViewStylePlain];
    [self.view addSubview:self.table];
    self.table.delegate = self;
    
    self.table.dataSource = self;
    
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.table release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    
    if (((NSArray *)[[[self.array objectAtIndex:section ]objectForKey:@"component"]objectForKey:@"imgs"]).count == 0) {
        return ((NSArray *)[[[self.array objectAtIndex:section ]objectForKey:@"component"]objectForKey:@"description"]).count;
    }else
    {
        return ((NSArray *)[[[self.array objectAtIndex:section ]objectForKey:@"component"]objectForKey:@"imgs"]).count  + ((NSArray *)[[[self.array objectAtIndex:section ]objectForKey:@"component"]objectForKey:@"description"]).count;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sss"];
    if (cell == nil) {
        cell = [[CommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"sss"];
    }
    //[NSString stringWithFormat:@"%ld",indexPath.row]
    
        if (indexPath.row < ((NSArray *)[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"description"]).count) {
            [cell.date setTitle:[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"publishDate"] forState:UIControlStateNormal];
            cell.username.text = [[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"userName"];
            cell.content.text = [[[[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"description"]objectAtIndex:indexPath.row]objectForKey:@"component"]objectForKey:@"text"];
            [cell.avatar sd_setImageWithURL:[NSURL URLWithString:[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"userAvatar"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
            cell.floor.text = [[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"floor"];

            return cell;
        }
        else
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%ldd", indexPath.section + indexPath.row]];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%ld", indexPath.section + indexPath.row]];
            }
            CGFloat width = [[[[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"imgs"]objectAtIndex:indexPath.row - ((NSArray *)[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"description"]).count]objectForKey:@"width"]floatValue];
            CGFloat height = [[[[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"imgs"]objectAtIndex:indexPath.row - ((NSArray *)[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"description"]).count]objectForKey:@"height"]floatValue];
            
            
            CGFloat hieght = self.view.frame.size.width * height / width;
            if (hieght <= 0) {
                hieght = 300 *_offHeight;
            }else
            {
                
                hieght = self.view.frame.size.width * height / width;
            }

            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, hieght)];
            [cell.contentView addSubview:image];
            [image sd_setImageWithURL:[NSURL URLWithString:[[[[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"imgs"]objectAtIndex:indexPath.row - ((NSArray *)[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"description"]).count]objectForKey:@"component"]objectForKey:@"picUrl"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
                return cell;
        }
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < ((NSArray *)[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"description"]).count) {
       return [[self class]heightWithText:[[[[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"description"]objectAtIndex:indexPath.row]objectForKey:@"component"]objectForKey:@"text"]] + 100 *_offHeight;

    }else
    {
        CGFloat width = [[[[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"imgs"]objectAtIndex:indexPath.row - ((NSArray *)[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"description"]).count]objectForKey:@"width"]floatValue];
        CGFloat height = [[[[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"imgs"]objectAtIndex:indexPath.row - ((NSArray *)[[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"description"]).count]objectForKey:@"height"]floatValue];
        
       
        CGFloat hieght = self.view.frame.size.width * height / width + 20;
        if (hieght <= 0) {
            return 300 *_offHeight;
        }else
        {
            
            return hieght;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}
#pragma mark -
#pragma mark - 计算文字高度
+ (CGFloat)heightWithText:(NSString *)text
{
    // 参数1: 预定的矩形大小
    // 参数2: 图形选项
    // 参数3: 属性字典(设置字体大小, 一般和cell中的设置成一样的)
    // 参数4: 内容文本 一般设置为空nil
    CGRect rect = [text boundingRectWithSize:CGSizeMake(280 * [[UIScreen mainScreen]bounds].size.width / 375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return rect.size.height;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
